using System;
using THANHTUNGO.Portal.Components.Security;

namespace MONA.Modules.Marketing.Controls
{
    public partial class AddEditService : AddEditItemControl
    {

        public event EventHandler SaveClick;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            UpdateBar.Visible = this.ShowUpdateBar;
            btnDelete.Visible = this.ItemID > 0;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    BLL.Service service = BLL.Service.Load(this.ItemID);
                    if (service != null)
                    {
                        txtServiceName.Text = service.Name;
                        ddlPriority.SelectedValue = service.PriorityID.ToString();
                        txtDescription.Text = service.Description;
                        ddlStatus.SelectedValue = service.StatusID.ToString();
                        chkIsClose.Checked = service.IsClosed;
                        ddlCustomer.SelectedValue = service.CustomerID.ToString();
                        ddlCustomerPM.SelectedValue = service.CustomerProjectManagerID.ToString();
                        ddlPM.SelectedValue = service.ProjectManagerID.ToString();
                        ddlAssigned.SelectedValue = service.AssignedToUserID.ToString();
                        ddlServiceType.SelectedValue = service.ServiceTypeID.ToString();
                        ddlServiceHistory.SelectedValue = service.ServiceHistoryID.ToString();
                        chkDeleted.Checked = service.IsDeleted;
                        txtPrice.Amount = service.Price;
                        txtMinPrice.Amount = service.MinPrice;
                        txtMaxPrice.Amount = service.MaxPrice;
                        txtToDate.Text = service.ToDate.ToShortDateString();
                        txtFromDate.Text = service.FromDate.ToShortDateString();
                        txtSummary.Text = service.Summary;
                    }
                }
            }
        }

        public bool Save()
        {
            int returnId = 0;
            if (Page.IsValid)
            {
                BLL.Service service = new BLL.Service();
                service.Name = txtServiceName.Text.Trim();
                service.PriorityID = Convert.ToInt32(ddlPriority.SelectedValue);
                service.Description = txtDescription.Text.Trim();
                service.StatusID = Convert.ToInt32(ddlStatus.SelectedValue);
                service.CustomerID = Convert.ToInt32(ddlCustomer.SelectedValue);
                service.CustomerProjectManagerID = Convert.ToInt32(ddlCustomerPM.SelectedValue);
                service.ProjectManagerID = Convert.ToInt32(ddlPM.SelectedValue);
                service.AssignedToUserID = Convert.ToInt32(ddlAssigned.SelectedValue);
                service.ServiceTypeID  = Convert.ToInt32(ddlServiceType.SelectedValue);                
                service.ServiceHistoryID = Convert.ToInt32(ddlServiceHistory.SelectedValue);
                service.IsClosed = chkIsClose.Checked;
                service.IsDeleted = chkDeleted.Checked;
                service.Price = txtPrice.Amount;
                service.MinPrice = txtMinPrice.Amount;
                service.MaxPrice = txtMaxPrice.Amount;
                service.ToDate = DateTime.Parse(txtToDate.Text);
                service.FromDate = DateTime.Parse(txtFromDate.Text);
                service.Summary = txtSummary.Text.Trim();
                service.CreatedByUserID = Int32.Parse(PortalSecurity.CurrentUser.Identity.ID);
                service.CreatedDate = DateTime.Now;
                service.LastUpdatedByUserID = Int32.Parse(PortalSecurity.CurrentUser.Identity.ID);
                service.LastUpdatedDate = DateTime.Now;
                if (this.ItemID == 0)
                {
                    returnId = service.Insert();
                }
                else
                {
                    service.ID = this.ItemID;
                    returnId = service.Update();
                }

                // Clear data in controls.
                txtServiceName.Text = txtDescription.Text = string.Empty;
                ddlPriority.SelectedIndex = ddlCustomer.SelectedIndex = ddlCustomerPM.SelectedIndex = ddlAssigned.SelectedIndex = ddlServiceType.SelectedIndex = ddlServiceHistory.SelectedIndex = 0;
                chkDeleted.Checked = false;
                txtPrice.Amount = txtMinPrice.Amount = txtMaxPrice.Amount = 0;

            }
            return returnId > 0;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.ReturnToReferrerPage();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                BLL.Service service = new BLL.Service();
                service.ID = this.ItemID;
                service.Delete();
                this.ReturnToReferrerPage();
            }
        }

        protected void AddEditCustomer_SaveClick(object sender, EventArgs e)
        {
            ddlCustomer.DataBind();
        }

        protected void AddEditContact_SaveClick(object sender, EventArgs e)
        {
            ddlCustomerPM.DataBind();
        }

        protected void AddEditAction_SaveClick(object sender, EventArgs e)
        {
            ddlNextAction.DataBind();
        }

        protected void AddEditServiceType_SaveClick(object sender, EventArgs e)
        {
            ddlServiceType.DataBind();
        }

        protected void AddEditEmployee_SaveClick(object sender, EventArgs e)
        {
            ddlAssigned.DataBind();
            ddlPM.DataBind();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Save();
            if (SaveClick != null)
            {
                SaveClick(sender, e);
            }

            this.ReturnToReferrerPage();
        }
    }
}